<?php

namespace app\controller;


use app\service\MemberWalletService;
use app\service\WithdrawalService;
use const support\ERROR;
use const support\LIMIT;
use const support\OPERATE_ERROR;
use const support\PAGE;

class WithdrawalController
{

    //提现明细
    public function withdrawalLists(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'page'	=>PAGE,
            'limit'	=>LIMIT
        ]);
        try {
            $info=WithdrawalService::withdrawalDetails(getMemberId(),$params['page'],$params['limit']);
            return buildSuccess($info);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //提现操作
    public function toApplyWithdrawal(): \support\Response
    {
        try {
            $params=getRequestData([
                'withdrawal_amount'	=>0,
                'bank_name'         =>'',
                'bank_number'       =>'',
                'bank_region'       =>'',
                'bank_branch'       =>'',
                'real_name'         =>''
            ]);
            $ret=WithdrawalService::toWithdrawalOperation(getMemberId(),$params);
            if (!$ret){
                return buildFail(OPERATE_ERROR,'提现失败');
            }
            return buildSuccess();
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //提现配置
    public function toWithdrawalConfig(): \support\Response
    {
        try {
            //获取用户当前可提现金额
            $balance=MemberWalletService::getWithdrawAbleAmount(getMemberId());
            //获取当前手续费配置信息
            $serviceCharge=0;
            return buildSuccess([
                'balance'=>$balance,
                'withdrawal_amount'=>$balance,
                'service_charge'=>$serviceCharge
            ]);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }


}